var Label = function(x, y, w, h)
{
	this.x = x;
	this.y = y;
	this.width = w;
	this.height = h;
	this.init();
}

// Inherit from graphical object
Label.prototype = new GraphicalObject();

Label.prototype.init = function()
{
	GraphicalObject.prototype.init.call(this);
	this.text = "Test This Hello Man There is Cool Lorum Ipsum mkfdjhjhjjgjnjnfddjkfnkdfgjkndfgkjndfgjk";
	this.fontStyle = "15px verdana";	// can be a regular CSS style string
	this.fontSize = "15";
	this.fontMeasurement = "px";
	this.font = "Verdana";
	this.fontStandardStyle = "bold";
	this.lineHeight = 14;
	this.hAlign = TextAlignment.Center;
	this.vAlign = TextAlignment.Middle;
	this.textAlign = "center";
	this.recalcAlignFlag = true;
	this.drawX = 0;
	this.drawY = 0;
	this.rotation = 0;
	this.lines = new Array();
	
	this.textWidth = -1;
	this.textHeight = -1;
	//this.createCanvas();
}

Label.prototype.createCanvas = function()
{
	var canvas;
	canvas = document.createElement('canvas');
	canvas.id = "labelcanvas_" + this.getId();
	document.body.appendChild(canvas);
	this.canvas = canvas;
	this.drawToCanvas(canvas.getContext('2d'), this.text);
}

Label.prototype.drawToCanvas = function(context, text)
{
	var dimensions = this.getTextDrawDimensions(context);
	var lineArray = this.splitTextIntoWords(text);
	
	dimensions.width = this.width;
	dimensions.height = this.height;
	
	this.canvas.width = dimensions.width;
	this.canvas.height = dimensions.height;
	context.clearRect(0, 0, this.canvas.width, this.canvas.height);	
	
	var formattedLines = this.convertWordsToLines(context, lineArray, this.canvas.width);
	this.drawLinesToCanvas(context, formattedLines, this.canvas.width);
}

Label.prototype.splitTextIntoWords = function(text)
{
	return text.split(' ');
}
// Sets
Label.prototype.setWidth = function(w)
{
	this.width = w;
}

Label.prototype.setHeight = function(h)
{
	this.height = h;
}

Label.prototype.setColor = function(c)
{
	this.color = c;
}

Label.prototype.setText = function(t)
{
	this.text = t;
}

Label.prototype.setStyle = function(s)
{
	if (s == undefined){s="";}
	this.fontStandardStyle = s;
}

Label.prototype.setHAlign = function(a)
{
	if (a != this.hAlign)
	{
		this.setRecalc();
	}
	this.hAlign = a;
}

Label.prototype.setVAlign = function(a)
{
	if (a != this.vAlign)
	{
		this.setRecalc();
	}
	this.vAlign = a;
}

Label.prototype.setRecalc = function()
{
	this.recalcAlignFlag = true;
}

Label.prototype.setFontSize = function(fs)
{
	this.fontSize = fs;
}

Label.prototype.setFontMeasurement = function(m)
{
	this.fontMeasurement = m;
}

Label.prototype.setFont = function(f)
{
	this.font = f;
}

Label.prototype.setLineHeight = function(lh)
{
	this.lineHeight = lh;
}


// Gets
Label.prototype.getWidth = function()
{
	return this.width;
}

Label.prototype.getHeight = function()
{
	return this.height;
}

Label.prototype.getColor = function()
{
	return this.color;
}

Label.prototype.getText = function()
{
	return this.text;
}

Label.prototype.getStyle = function()
{
	return this.fontStandardStyle;
}

Label.prototype.getVAlign = function()
{
	return this.vAlign;
}

Label.prototype.getHAlign = function()
{
	return this.hAlign;
}

Label.prototype.getCompiledStyleString = function()
{
	if (this.fontSize == undefined || this.fontSize == null) this.fontSize = "10";
	
	var str = this.fontSize.concat(this.fontMeasurement + " ", this.font + " ", this.fontStandardStyle);
	return str;
}

Label.prototype.getFontSize = function()
{
	return this.fontSize;
}

Label.prototype.getLineHeight = function()
{
	return this.lineHeight;
}

Label.prototype.drawWrappedText = function( context, text, x, y, lineHeight, fitWidth)
{
	fitWidth = fitWidth || 0;

	if (fitWidth <= 0)
	{
		context.fillText( text, x, y );
		return;
	}

	var words = text.split(' ');
	var currentLine = 0;
	var idx = 1;

	while (words.length > 0 && idx <= words.length)
	{
		var str = words.slice(0,idx).join(' ');
		var w = context.measureText(str).width;
			
		if ( w > fitWidth )
		{
			if (idx==1)
			{
				idx=2;
			}
			context.fillText( words.slice(0,idx-1).join(' '), x, y + (lineHeight*currentLine) );
			currentLine++;
			words = words.splice(idx-1);
			idx = 1;
		}
		else
		{
			idx++;
		}
	}
	if (idx > 0)
	{
		context.fillText( words.join(' '), x, y + (lineHeight*currentLine) );
	}
}
/*
Label.prototype.calcMultilineText = function(text, lineHeight)
{
	var newlineIndex = text.indexOf('\n');
	var currentLine = 0;
	var index = 0;
	var lineArray = new Array();
	var str = text;
	
	if (newlineIndex == -1)
	{
		lineArray.push(str);
	}
	else
	{
		lineArray.push(str.substr(0,newlineIndex));
		
		newlineIndex = str.indexOf('\n');
		
		while(newlineIndex != -1)
		{
			str = str.substr(newlineIndex+1, str.length-(newlineIndex+1));
			lineArray.push(str);
			newlineIndex = str.indexOf('\n');
		}
	}
	return lineArray;
}
*/
Label.prototype.calcMultilineText = function(text, lineHeight)
{
	var startIndex = 0;
	var endIndex = text.indexOf('|');
	var lineArray = new Array();
	var str = text;
	var seperators = null;
	var lines = new Array();
	var substr;
	var addRemainder = false;
	if (endIndex != -1)
	{
		substr = str.substr(0, endIndex-1);
		lines.push(substr);
		seperators = findSeperators(str, '|');
		addRemainder = true;
		for(var i=0;i<seperators.length-1;i+=1)
		{
			substr = str.substr(seperators[i]+1, (seperators[i+1] - seperators[i])-1);
			console.log("EXTRACT:" + substr);
			lines.push(substr);
		}
		
		if (i != 0 || addRemainder == true)
		{
			substr = str.substr(seperators[i]+1, (str.length - seperators[i])-1);
			lines.push(substr);
		}
	}
	else
	{
		lines.push(str);
	}
	return lines;
}


Label.prototype.convertWordsToLines = function(context, wordsArray, maxWidth)
{

	var str = "";
	var array = new Array();
	var index = 0;
	var startIndex = 0;
	
	for(var i=0;i<=wordsArray.length; i+=1)
	{
		var word = wordsArray[index];
		var strwidth = context.measureText(str + word).width;
		var wordwidth = context.measureText(word).width;
		
		if (strwidth > maxWidth)
		{
			if (index == 0){ str = word; };
			var line = {
				str:	str,
				width: 	strwidth-wordwidth
			}
			
			array.push(line);
			startIndex = index+1;
			str = "";
		}
		else
		{
			str += word + " ";
			index++;
		}
	}
	
	
	/*
	while(wordsArray.length > 0 && index <= wordsArray.length)
	{
		var word = wordsArray[index];
		var strwidth = context.measureText(str + word).width;
		var wordwidth = context.measureText(word).width;
		if (strwidth > maxWidth)
		{
			if (index == 0){ str = word; };
			var line = {
				str:	str,
				width: 	strwidth-wordwidth
			}
			
			array.push(line);
			wordsArray = wordsArray.splice(index-1);
			str = "";
			newLine = 1;
		}
		else
		{
			str += word + " ";
			index++;
		}
	}
	*/
	if (index > 0)
	{
		var line = {
			str:	str,
			width: 	strwidth-wordwidth
		}
		array.push(line);
	}
	
	return array;
}

Label.prototype.drawLine = function(x, y, line, context, width)
{
	var lx;
	switch(this.getHAlign())
	{
		case TextAlignment.Left:
			lx = 0;
		break;
		
		case TextAlignment.Right:
			lx = width - line.width;
		break;
		
		case TextAlignment.Center:
			lx = (width/2) - (line.width/2);
		break;
	}
	
	context.fillText( line.str, x + lx, y);
}

Label.prototype.drawTextLine = function(context, text, x, y)
{
	var lx;
	var textWidth;
	var textHeight;
	context.font = this.getCompiledStyleString();
	
	textWidth = context.measureText(text).width;
	
	switch(this.getHAlign())
	{
		case TextAlignment.Left:
			lx = 0;
		break;
		
		case TextAlignment.Right:
			lx = -textWidth;
		break;
		
		case TextAlignment.Center:
			lx = -textWidth/2;
		break;
	}
	
	context.fillText( line, x + lx, y);
}

Label.prototype.drawLinesToCanvas = function(context, lineArray, x, y)
{
	for(var i=0;i<lineArray.length;i+=1)
	{
		line = lineArray[i];
		this.drawTextLine(context, line, x, y+(this.getLineHeight()*i));
		//context.fillText(line, x, y+(this.getLineHeight()*i));
	}
}

Label.prototype.getTextDrawDimensions = function(context)
{
	fitWidth = this.width;

	var words = this.text.split(' ');
	var currentLine = 0;
	var idx = 1;
	
	var maxWidth = 0;
	var maxHeight = 0;

	while (words.length > 0 && idx <= words.length)
	{
		var str = words.slice(0,idx).join(' ');
		var w = context.measureText(str).width;

		if (w > maxWidth)
		{
			maxWidth = w;
		}

		if ( w > fitWidth )
		{
			if (idx==1)
			{
				idx=2;
			}

			currentLine++;
			words = words.splice(idx-1);
			idx = 1;
		}
		else
		{
			idx++;
		}
	}

	maxHeight = this.getLineHeight() * (currentLine+1);
	
	return {
		width:	maxWidth,
		height:	maxHeight
	}

}
Label.prototype.getTextBounds = function(context)
{
	var width;
	var height;
	var measureWidth;
	var line;
	
	for(var i=0; i<this.lines.length;i+=1)
	{
		line = this.lines[i];
		measureWidth = context.measureText(line).width;
		if (measureWidth > width)
		{
			width = measureWidth;
		}
	}
	
	height = (i+1) *getFontPixelHeight(this.getFontSize());
	
	return {
		width:	width,
		height: height
	}
}
Label.prototype.decideDrawPosition = function(context)
{
	var tX;
	var tY;
	
	var textRegion = this.getTextBounds(context);
	
	switch(this.getHAlign())
	{
		case TextAlignment.Left:
			tX = 0;
			//this.textAlign = "right";
		break;
		
		case TextAlignment.Center:
			tX = (this.width/2);
			//this.textAlign = "center";
		break;
		
		case TextAlignment.Right:
			tX = this.width;
			//this.textAlign = "left";
		break;
	}
	
	switch(this.getVAlign())
	{
		case TextAlignment.Top:
			tY = textRegion.height;
		break;
		
		case TextAlignment.Middle:
			tY = this.height/2 - (textRegion.height/2);
		break;
		
		case TextAlignment.Bottom:
			//console.log("textregion.height = " + textRegion.height);
			tY =  this.height - textRegion.height;
		break;
	}
	
	return {
		x:	tX,
		y:	tY
	};
}

Label.prototype.setTextDrawPosition = function(vector)
{
	this.drawX = vector.x;
	this.drawY = vector.y;
}

Label.prototype.drawBounds = function(context, bounds)
{
	context.fillStyle = Color.None;
	context.beginPath();
	context.rect(this.x, this.y, this.width, this.height);
	context.closePath();
	
	context.strokeStyle = Color.Red;
	context.stroke();
}

Label.prototype.getBounds = function()
{
	return {
		x: this.x,
		y: this.y,
		width: this.width,
		height: this.height
	};
}
// Draw function
Label.prototype.draw = function(context)
{
	//this.drawBounds(context, this.getBounds());
	var textSizeOriginal = this.fontSize;
	var textSizeNew = parseFloat(this.fontSize);
	
	//context.textAlign = this.textAlign;
	context.fillStyle = this.color;
	context.font = this.getCompiledStyleString();
	
	//context.font = context.font.replace(textSizeOriginal, textSizeNew);
	
	if (this.recalcAlignFlag == true)
	{
		this.recalcAlignFlag = false;
		this.setTextDrawPosition(this.decideDrawPosition(context));
		this.lines = this.calcMultilineText(this.text, this.getLineHeight());
		//this.drawToCanvas(context, this.text, this.lineHeight, this.width);
	}
	this.drawLinesToCanvas(context, this.lines, this.x + this.drawX, this.y + this.drawY);
	//this.drawCanvasToMainCanvas(context, this.x, this.y);
	//this.drawWrappedText(context, this.text, this.x + this.drawX, this.y + this.drawY, this.lineHeight, this.width);
}

Label.prototype.drawCanvasToMainCanvas = function(context, x, y)
{
	context.drawImage(this.canvas, x, y);
}
